package Kuaishou.study.order;

/**
 * @author leiwenkang <leiwenkang@kuaishou.com>
 * Created on 2022-01-10
 */
public class BubbleSort {
    public static void main(String[] args) {
        int[] array = {3, 5, 2, 7, 8, 6, 4, 9, 1};
        new BubbleSort().sort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            System.out.print(" ");
        }
    }

    public void sort(int[] array) {
        if (array == null || array.length == 0) {
            return;
        }
        for (int i = 0; i < array.length; i++) {
            for (int j = array.length - 1; j > i; j--) {
                if (array[j] < array[j - 1]) {
                    //交换
                    swap(array, j ,j - 1);
                }
            }
        }
    }

    public void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
